package jzqb.service.impl;

import haha.life.common.CutPageBean;
import haha.life.normal.ParseMoninorArgsUtil;
import jzqb.bo.AdminQueryBo;
import jzqb.bo.MonitorBo;
import jzqb.dao.AdminLogDao;
import jzqb.dao.AdminUserInfoDao;
import jzqb.po.*;
import jzqb.service.AdminLogService;
import jzqb.vo.StandardStatisticsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


/**
 * 后台日志统计业务层实现类
 */
@Service
public class AdminLogServiceImpl implements AdminLogService {

    @Autowired
    private AdminLogDao adminLogDao;

    @Autowired
    private AdminUserInfoDao adminUserInfoDao;

    @Override
    public List<StandardStatisticsVo> StandardStatistics(AdminQueryBo bo){
        List<StandardStatisticsVo> list = null;//存放数据查询结果
        HashMap<String, Integer> browseMap = new HashMap<>();//存放浏览量键值对
        int id = 1;//ID值
        //1.根据传入的参数决定统计的方式
        if(1 == bo.getGroupWay()){
            list = adminLogDao.StandardStatisticsByOne(bo);//按单条统计
            //循环取出表名与标识符拼接，对全局唯一标识进行赋值
            String overallID = "";
            String getBrowseID = "";
            for (StandardStatisticsVo vo : list) {
                if("JZQBBK_METADATA".equals(vo.getTablename())){
                    String publishdate = vo.getPublishdate();
                    String author = vo.getAuthor();
                    //发表时间格式统一
                    if(!"".equals(publishdate) && publishdate != null){
                        vo.setPublishdate(publishdate.substring(0,10));
                    }
                    //作者格式统一
                    if(!"".equals(author) && author != null){
                        vo.setAuthor(author.split("_")[1]);
                    }
                }
                if (!"".equals(vo.getSysid()) && vo.getSysid() != null && vo.getSysid() != 0) {//判断唯一值是用表名+ID还是表名+文件名
                    overallID = vo.getTablename() + "_" + vo.getSysid();
                } else {
                    overallID = vo.getTablename() + "_" + vo.getFilename();
                }
                vo.setId(id++);//设置导出时的ID
                vo.setOverallid(overallID);
                getBrowseID += overallID +",";
            }
            //放入浏览数
            List<VisitPo> poList = adminLogDao.getBrowseByOverallID(getBrowseID);
            for (VisitPo visitPo : poList) {
                browseMap.put(visitPo.getOverallid(),visitPo.getBrowse());
            }
            for (StandardStatisticsVo vo : list) {
                Integer integer = browseMap.get(vo.getOverallid());
                if(integer == null || "".equals(integer)) integer = 0;
                vo.setBrowse(integer);
            }
        }else{
            //按数据库统计
            list = adminLogDao.StandardStatisticsByCondition(bo);//按数据库查询
            if(bo.getDbCode().split(",").length == 1) bo.setDbTable(bo.getDbCode());
            List<StandardStatisticsVo> browseList = adminLogDao.getBrowseByCondition(bo);//存放浏览量查询结果
            for (StandardStatisticsVo vo : browseList) {
                browseMap.put(vo.getDbTable(),vo.getTotalBROWSERATE());
            }
            for (StandardStatisticsVo vo : list) {
                vo.setTotalBROWSERATE(browseMap.get(vo.getTablename()));
            }
        }
        return list;
    }

    @Override
    public Long count(AdminQueryBo bo) {
        Long total = 0L;
        if(1 == bo.getGroupWay()){
             total = adminLogDao.countAll(bo);
        }else{
            total = Long.valueOf(adminLogDao.countPart(bo).size());
        }
        return total;
        
    }

    @Override
    public List<StandardStatisticsVo> exportStandardStatistics1(AdminQueryBo bo) {
        List<StandardStatisticsVo> list = null;//存放数据查询结果
        List<StandardStatisticsVo> browseList = null;//存放浏览量查询结果
        HashMap<String, Integer> browseMap = new HashMap<>();//存放浏览量键值对
        int id = 1;//ID值
        //1.根据传入的参数决定统计的方式
        if(1 == bo.getGroupWay()){
            list = adminLogDao.export(bo);//按单条统计
//            list = adminLogDao.StandardStatisticsByOne(bo);//按单条统计

            //循环取出表名与标识符拼接，对全局唯一标识进行赋值
            String overallID = "";
            String getBrowseID = "";
            for (StandardStatisticsVo vo : list) {
                if("JZQBBK_METADATA".equals(vo.getTablename())){
                    String publishdate = vo.getPublishdate();
                    String author = vo.getAuthor();
                    //发表时间格式统一
                    if(!"".equals(publishdate) && publishdate != null){
                        vo.setPublishdate(publishdate.substring(0,10));
                    }
                    //作者格式统一
                    if(!"".equals(author) && author != null){
                        vo.setAuthor(author.split("_")[1]);
                    }
                }
                if (!"".equals(vo.getSysid()) && vo.getSysid() != null && vo.getSysid() != 0) {//判断唯一值是用表名+ID还是表名+文件名
                    overallID = vo.getTablename() + "_" + vo.getSysid();
                } else {
                    overallID = vo.getTablename() + "_" + vo.getFilename();
                }
                vo.setId(id++);//设置导出时的ID
                vo.setOverallid(overallID);
                getBrowseID += overallID +",";
                if(id % 500 == 0){
                    List<VisitPo> poList = adminLogDao.getBrowseByOverallID(getBrowseID);
                    for (VisitPo visitPo : poList) {
                        browseMap.put(visitPo.getOverallid(),visitPo.getBrowse());
                    }
                   getBrowseID = null;
                }
            }
            //放入浏览数
//            List<VisitPo> poList = adminLogDao.getLongBrowseByOverallID(getBrowseID);
            if(getBrowseID != null){
                List<VisitPo> poList = adminLogDao.getBrowseByOverallID(getBrowseID);
                for (VisitPo visitPo : poList) {
                    browseMap.put(visitPo.getOverallid(),visitPo.getBrowse());
                }
            }
            for (StandardStatisticsVo vo : list) {
                Integer integer = browseMap.get(vo.getOverallid());
                if(integer == null || "".equals(integer)) integer = 0;
                vo.setBrowse(integer);
            }
        }else{
            //按数据库统计
            list = adminLogDao.StandardStatisticsByCondition(bo);//按数据库查询
            if(bo.getDbCode().split(",").length == 1) bo.setDbTable(bo.getDbCode());
            browseList = adminLogDao.getBrowseByCondition(bo);
            for (StandardStatisticsVo vo : browseList) {
                browseMap.put(vo.getDbTable(),vo.getTotalBROWSERATE());
            }
            for (StandardStatisticsVo vo : list) {
                vo.setId(id++);
                vo.setTotalBROWSERATE(browseMap.get(vo.getTablename()));
            }
        }
        return list;
    }

    @Override
    public boolean insertLogRecord(AdminViewDownLogPo po){
        return adminLogDao.insertLogRecord(po);
    }

    @Override
    public CutPageBean<AdminViewDownLogPo> cutPage (AdminViewDownLogPo adminViewDownLogPo){
        CutPageBean<AdminViewDownLogPo> cutPageBean = new CutPageBean<>();
        List<AdminViewDownLogPo> adminViewDownLogPoList  = adminLogDao.cutList(adminViewDownLogPo);

//        //特殊处理: 4_情报自用 为 情报自用
//        for ( AdminViewDownLogPo po:adminViewDownLogPoList){
//            if (po.getSource().split("_").length==2){
//                String newSourceStr = po.getSource().split("_")[1];
//                po.setSource(newSourceStr);
//            }
//        }


        //获取所有的用户
        List<AdminUserPo> allUser = adminUserInfoDao.findAllUser();
        for (int i = 0; i < adminViewDownLogPoList.size(); i++) {
            AdminViewDownLogPo logPo = adminViewDownLogPoList.get(i);
            for (int j = 0; j < allUser.size(); j++) {
                AdminUserPo userPo = allUser.get(j);
                if (userPo.getId().equals(logPo.getUserID()) ){
                    logPo.setUserName(userPo.getName());
                    break;
                }
            }
        }


        int count=(int) adminLogDao.cutCount(adminViewDownLogPo);
        cutPageBean.initCutPage(count, adminViewDownLogPo.getPageSize(),adminViewDownLogPoList);
        return cutPageBean;
    }

    @Override
    public List<AdminViewDownLogPo> getResNotPaged(AdminViewDownLogPo adminViewDownLogPo){
        List<AdminViewDownLogPo> res = adminLogDao.getResNotPaged(adminViewDownLogPo);
        //特殊处理: 4_情报自用 为 情报自用
        for ( AdminViewDownLogPo po:res){
            if (po.getSource().split("_").length==2){
                String newSourceStr = po.getSource().split("_")[1];
                po.setSource(newSourceStr);
            }
        }

        //获取所有的用户
        List<AdminUserPo> allUser = adminUserInfoDao.findAllUser();
        for (int i = 0; i < res.size(); i++) {
            AdminViewDownLogPo logPo = res.get(i);
            for (int j = 0; j < allUser.size(); j++) {
                AdminUserPo userPo = allUser.get(j);
                if (userPo.getId().equals(logPo.getUserID()) ){
                    logPo.setUserName(userPo.getName());
                    break;
                }
            }
        }
        return res;
    }

    @Override
    public CutPageBean<AdminLoginLogPo> cutPage_login(AdminLoginLogPo adminLoginLogPo){
        CutPageBean<AdminLoginLogPo> cutPageBean = new CutPageBean<>();
        List<AdminLoginLogPo> adminLoginLogPoList  = adminLogDao.cutList_login(adminLoginLogPo);

        //获取所有的用户
        List<AdminUserPo> allUser = adminUserInfoDao.findAllUser();
        for (int i = 0; i < adminLoginLogPoList.size(); i++) {
            AdminLoginLogPo logPo = adminLoginLogPoList.get(i);
            for (int j = 0; j < allUser.size(); j++) {
                AdminUserPo userPo = allUser.get(j);
                if (userPo.getId().equals(logPo.getUserid()) ){
                    logPo.setUserName(userPo.getName());
                    break;
                }
            }
        }

        int count=(int) adminLogDao.cutCount_login(adminLoginLogPo);
        cutPageBean.initCutPage(count, adminLoginLogPo.getPageSize(),adminLoginLogPoList);
        return cutPageBean;
    }

    @Override
    public List<AdminLoginLogPo> getResNotPaged_login(AdminLoginLogPo adminLoginLogPo){

        List<AdminLoginLogPo> res = adminLogDao.getResNotPaged_login(adminLoginLogPo);
        //获取所有的用户
        List<AdminUserPo> allUser = adminUserInfoDao.findAllUser();
        for (int i = 0; i < res.size(); i++) {
            AdminLoginLogPo logPo = res.get(i);
            for (int j = 0; j < allUser.size(); j++) {
                AdminUserPo userPo = allUser.get(j);
                if (userPo.getId().equals(logPo.getUserid()) ){
                    logPo.setUserName(userPo.getName());
                    break;
                }
            }
        }
        return res;
    }

    @Override
    public CutPageBean<MonitorPo> cutPage_Monitor(MonitorBo bo){
        CutPageBean<MonitorPo> cutPageBean = new CutPageBean<>();
        List<MonitorPo> MonitorPoList  = adminLogDao.cutPage_Monitor(bo);

        //获取所有的用户
        List<AdminUserPo> allUser = adminUserInfoDao.findAllUser();
        for (int i = 0; i < MonitorPoList.size(); i++) {
            MonitorPo logPo = MonitorPoList.get(i);
            for (int j = 0; j < allUser.size(); j++) {
                AdminUserPo userPo = allUser.get(j);
                if (userPo.getId().equals(logPo.getUserid()) ){
                    logPo.setName(userPo.getName());
                    break;
                }
            }
        }
        int count=(int) adminLogDao.cutCount_Monitor(bo);

        //生成参数描述
        MonitorPoList = ParseMoninorArgsUtil.ParseOperation(MonitorPoList);

        cutPageBean.initCutPage(count, bo.getPageSize(),MonitorPoList);
        return cutPageBean;
    }


    @Override
    public List<MonitorPo> exportMonitorLog(MonitorBo bo){
        List<MonitorPo> res = adminLogDao.exportMonitorLog(bo);


        //获取所有的用户
        List<AdminUserPo> allUser = adminUserInfoDao.findAllUser();
        for (int i = 0; i < res.size(); i++) {
            MonitorPo po = res.get(i);
            for (int j = 0; j < allUser.size(); j++) {
                AdminUserPo userPo = allUser.get(j);
                if (userPo.getId().equals(po.getUserid()) ){
                    po.setName(userPo.getName());
                    break;
                }
            }
        }

        res = ParseMoninorArgsUtil.ParseOperation(res);
        return res;
    }
}
